#include "base/log.h"
#include <unistd.h>
#include "net/tcpclient.h"
#include "net/connect.h"
#include "base/config.h"
#include "base/logthread.h"
#include "MyRpc/message.h"
#include "MyRpc/rpc.h"

#include <sys/time.h>

myrpc::Message* message = nullptr;
myrpc::base::Rpc* rpc = nullptr;

void close_func(net_tools::net::Connect* connect){
  connect->Print_ip_port("close");
};
char buf[200] = {};
void read_func(net_tools::net::Connect* connect){
  connect->Read(buf,200);
  message->Consume_message(connect,buf,200);
};
void conn_func(net_tools::net::Connect* connect){
  connect->Print_ip_port("connect");
  connect->Set_read_callback(read_func);
  connect->Set_close_callback(close_func);
  NT_LOG_INFO << "connect ready" << NT_LOG_ENDL;
  sleep(1);
  NT_LOG_INFO << "begin" << NT_LOG_ENDL;
};
int d = 0;
void Test1(int a,int b,int c){
  // NT_LOG_INFO << a << NT_LOG_ENDL;
  // NT_LOG_INFO << b << NT_LOG_ENDL;
  // NT_LOG_INFO << c << NT_LOG_ENDL;
  ++d;
  if(d == 1000000) [[unlikely]] {
    NT_LOG_INFO << "end" << NT_LOG_ENDL;
  }
};

void Test2(int a){
  NT_LOG_INFO << a << NT_LOG_ENDL;
};

int main(int argc, char* argv[])
{
  net_tools::Config config("Client");
  net_tools::Log log_;
  message = myrpc::Message::Get_singleton();
  rpc = message->Get_rpc();
  net_tools::Tcpclient tcp;
  int c = 0;
  rpc->Init_fun(3,Test1,[](net_tools::net::Connect*,void*,unsigned int){NT_LOG_INFO << "rpc_test" << NT_LOG_ENDL;});
  rpc->Init_fun(2,[&](int a){NT_LOG_INFO << c << NT_LOG_ENDL;},[](net_tools::net::Connect*,void*,unsigned int){NT_LOG_INFO << "rpc_test" << NT_LOG_ENDL;});
  {
    auto fun = [&](int a)->void{
      ++c;
      NT_LOG_INFO << c + a << NT_LOG_ENDL;
    };
    rpc->Init_fun(1,fun,[](net_tools::net::Connect*,void*,unsigned int){NT_LOG_INFO << "rpc" << NT_LOG_ENDL;});
  }
  tcp.Set_connect_over_callback(conn_func);
  tcp.Startclient(true);
  NT_LOG_INFO << "pause" << NT_LOG_ENDL;
  pause();
};